package gepes.dao;

import gepes.exepcion.DAOExcepcion;
import gepes.modelo.Administrador;
import gepes.modelo.Usuario;
import gepes.util.ConexionBD;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class AdministradorDAO extends BaseDAO{
	public int existe(String campo, String dato) throws DAOExcepcion {
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {					
			String query = "select count(0) from administrador where "
					+ campo + "=?"; 		
			
			con = ConexionBD.ConectarDB();
			stmt = con.prepareStatement(query);
			stmt.setString(1, dato);
			rs = stmt.executeQuery();
			if (rs.next()) {
				return rs.getInt(1);
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return 0;

	}
	public Administrador login(Administrador us) throws DAOExcepcion{
		String query = "select nombres,correo,clave,codadministrador "
				+ " from administrador where "
				+ " correo=? and clave=?";
		Administrador vo = new Administrador();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.ConectarDB();
			stmt = con.prepareStatement(query);
			stmt.setString(1, us.getCorreo());
			stmt.setString(2, us.getClave());
			
			rs = stmt.executeQuery();
			if (rs.next()) {
				vo.setNombre(rs.getString(1));
				vo.setCorreo(rs.getString(2));
				vo.setClave(rs.getString(3));	
				vo.setCodadministrador(rs.getInt(4));
				return vo;
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {

			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}		
		return vo;
	}
	public Administrador insertar(Administrador ad) throws DAOExcepcion {
		String query = "insert into administrador(codadministrador,nombres,correo,clave)"
				+ "values(?,?,?,?)";
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.ConectarDB();
			stmt = con.prepareStatement(query);
			stmt.setInt(1, ad.getCodadministrador());
			stmt.setString(2, ad.getNombre());
			stmt.setString(3,ad.getCorreo());
			stmt.setString(4, ad.getClave());
			
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("Error insertar");
			}

			int id = 0;
			query = "select last_insert_id()";
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			if (rs.next()) {
				id = rs.getInt(1);
			}
			ad.setCodadministrador(id);
			
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return ad;
	}

}
